﻿CREATE PROCEDURE [dbo].[SaveWorkflow]
(@WorkflowID uniqueidentifier,
@WorkflowName varchar(255),
@FileName varchar(255),
@Version varchar(30),
@Author varchar(50),
@CreatedOn datetime,
@ModifiedOn datetime,
@Data xml)
AS
begin
if exists (select * from dbo.Workflow where WorkflowID=@WorkflowID) begin
	update dbo.Workflow 
	set 
		WorkflowName=@WorkflowName,
		FileName=@FileName,
		Version=@Version,
		Author=@Author,
		CreatedOn=@CreatedOn,
		ModifiedOn=@ModifiedOn,
		Data=@Data
	where
		WorkflowID=@WorkflowID;
end
else if exists (select * from dbo.Workflow where WorkflowName=@WorkflowName) begin
	delete dbo.Workflow where WorkflowName=@WorkflowName;
	insert into Workflow (
		WorkflowID,
		WorkflowName,
		FileName,
		Version,
		Author,
		CreatedOn,
		ModifiedOn,
		Data) 
	values (
		@WorkflowID,
		@WorkflowName,
		@FileName,
		@Version,
		@Author,
		@CreatedOn,
		@ModifiedOn,
		@Data);
end
else begin
	insert into Workflow (
		WorkflowID,
		WorkflowName,
		FileName,
		Version,
		Author,
		CreatedOn,
		ModifiedOn,
		Data) 
	values (
		@WorkflowID,
		@WorkflowName,
		@FileName,
		@Version,
		@Author,
		@CreatedOn,
		@ModifiedOn,
		@Data);
end
RETURN 0;
end